package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.sumpu.app.guoguojie.dao.TagDao;
import cn.sumpu.app.guoguojie.domain.Tag;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
public class TagDaoImpl extends SqlSessionDaoSupport implements TagDao {

	private static Logger logger = LoggerFactory.getLogger(TagDaoImpl.class);

	public void insertTag(Tag tag) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.TagMapper.insert",
					tag);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertTag database failure.", e);
			}
			throw new AppBusinessException("insertTag database failure.", e);
		}
	}

	public void deleteTag(Tag tag) throws AppBusinessException {
		try {
			getSqlSession().delete(
					"cn.sumpu.app.guoguojie.persistence.TagMapper.delete",
					tag);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteTag database failure.", e);
			}
			throw new AppBusinessException("deleteTag database failure.", e);
		}
	}

	public void updateTag(Tag tag) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.TagMapper.update",
					tag);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateTag database failure.", e);
			}
			throw new AppBusinessException("updateTag database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Tag> getTagList(Map<String, Object> map) throws AppBusinessException {
		try {
			return  getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.TagMapper.getTagList",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTagList database failure.", e);
			}
			throw new AppBusinessException("getTagList database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tag> getTagByDoorplate(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return  getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.TagMapper.getTagByDoorplate",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTagByDoorplate database failure.", e);
			}
			throw new AppBusinessException("getTagByDoorplate database failure.", e);
		}
	}

	@Override
	public Integer getTagByDoorplateCount(String doorplate)
			throws AppBusinessException {
		try {
			return  (Integer) getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.TagMapper.getTagByDoorplateCount",
					doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTagByDoorplateCount database failure.", e);
			}
			throw new AppBusinessException("getTagByDoorplateCount database failure.", e);
		}
	}

}
